package jcolorlinez;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class BoardFrame extends JFrame {
	private ChessPanel[][] chessPanels = null;
	public final Grid grid;
	public BoardFrame(Grid grid) {
		this.grid = grid;
		if (!grid.load()) {
			grid.init();
		}
		
		setLayout(new GridLayout(grid.getRows(), grid.getColumns()));

		chessPanels = new ChessPanel[grid.getRows()][grid.getColumns()];
		for (int row=0; row<grid.getRows(); row++) {
			for (int col=0; col<grid.getColumns(); col++) {
				chessPanels[row][col] = new ChessPanel(this, row, col);
				add(chessPanels[row][col]);
			}
		}
		
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				BoardFrame board = (BoardFrame)e.getComponent();
				board.grid.save();
				board.setVisible(false);
				board.dispose();
			}
		});
		this.setMinimumSize(new Dimension(400, 400));
		this.setTitle("Java Color Linez");
	}
}
